package com.runshu.newsmartark.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.runshu.newsmartark.R;
import com.runshu.newsmartark.base.MyApplication;
import com.runshu.newsmartark.base.TakeStockBase;
import com.runshu.newsmartark.dbutil.DBUtils;
import com.runshu.newsmartark.dbutil.LogListEntity;
import com.runshu.newsmartark.entity.ConfirmStockEntity;
import com.runshu.newsmartark.entity.HwInfoUtils;
import com.runshu.newsmartark.event.Event;
import com.runshu.newsmartark.event.EventBusUtils;
import com.runshu.newsmartark.event.EventCode;
import com.runshu.newsmartark.mvp.contract.ConfirmStockContract;
import com.runshu.newsmartark.mvp.model.ConfirmStockModel;
import com.runshu.newsmartark.mvp.presenter.ConfirmStockPresenter;
import com.runshu.newsmartark.network.netbean.ResponseBean;
import com.runshu.newsmartark.util.CountDownTimer;
import com.runshu.newsmartark.util.MacUtils;
import com.runshu.newsmartark.util.NetStatusUtils;
import com.runshu.newsmartark.util.RefUtil;
import com.runshu.newsmartark.util.SoundPoolUtils;
import com.runshu.newsmartark.util.common.ToastUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import static com.runshu.newsmartark.util.StringUtils.sdf;

/**
 * Demo class
 *
 * @author yu
 * @date 2021/12/28
 * @decs：
 */

public class NewEmbActivity extends TakeStockBase implements ConfirmStockContract.View {
    private int closeTime;
    private String macAddress;
    ArrayList<String> str = new ArrayList<>();
    private ConfirmStockPresenter confirmStockPresenter;
    private CountDownTimer timercloseDia;
    private CountDownTimer timer;

    TextView tv_count;
    ListView mListView;
    private Button btn_sure;
    //生成一个ArrayList类型的变量list
    SimpleAdapter adapter;
    private List<HashMap<String, String>> tagList = new ArrayList<HashMap<String, String>>();

    @Override
    protected int getLayoutId() {
        return R.layout.act_new_emb;
    }

    @Override
    protected void initView() {
        btn_sure = findViewById(R.id.btn_sure);
        mListView = (ListView) findViewById(R.id.listView1);
        tv_count = (TextView) findViewById(R.id.Epc_count);

        closeTime = 5000;
        macAddress = MacUtils.getFormatMacAddr();
        confirmStockPresenter = new ConfirmStockPresenter(this, new ConfirmStockModel());
        SoundPoolUtils.play(context, R.raw.raw_opean, 0);
        closeDoorHandler.sendEmptyMessageDelayed(0, closeTime);
    }


    @Override
    protected void initData() {

    }

    @Override
    protected void bindListener() {
        btn_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goMain();
            }
        });
    }

    @Override
    protected void afterRequestPermission(int requestCode, boolean isAllGranted) {

    }

    @Override
    public void onScanResult(List<HashMap<String, String>> lsTagList, boolean fromCloseDoor, long a, long b) {
        super.onScanResult(lsTagList, fromCloseDoor);
        tagList = lsTagList;
        adapter = new SimpleAdapter(this, tagList, R.layout.item,
                new String[]{"sn", "epc", "count", "rssi"}, new int[]{
                R.id.sn, R.id.epc, R.id.count, R.id.rssi});
        mListView.setAdapter(adapter);
        tv_count.setText("" + lsTagList.size());
        faultcloseDia();
        if (str == null) str = new ArrayList<>();
        str.clear();
        for (int i = 0; i < lsTagList.size(); i++) {
            str.add(RefUtil.decryptRfid(lsTagList.get(i).get("epc").replace(" ", "")));
        }
        if (NetStatusUtils.isConnected(context)) {
            confirmStockPresenter.checkInventory(HwInfoUtils.getCustomerId(context), HwInfoUtils.getHospitalId(context), macAddress, str, 0);

        } else {
            if (str.size() > 0) {
                LogListEntity logListEntity = new LogListEntity();
                String createTime = sdf.format(new Date());
                logListEntity.setCreateTime(createTime);
                logListEntity.setRfid(str);
                logListEntity.setUserCode(MyApplication.getUserInfoGlob().getUserCode());
                DBUtils.getInstance().getProAllDao().insertLogList(logListEntity);
            }
            EventBusUtils.sendEvent(new Event(EventCode.NetError));
            goMain();
        }

    }

    Handler closeDoorHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
//            closeDoor(100);
            return true;
        }
    });

    @Override
    public void onCloseDoor() {
        super.onCloseDoor();
    }

    @Override
    public void OnGetConfirmStockSuccess(ResponseBean responseBean) {
        ConfirmStockEntity StockInfo = (ConfirmStockEntity) responseBean.pullData();
        if (StockInfo != null)
            finish();
        SoundPoolUtils.play(context, R.raw.raw_date_complete, 0);


    }

    @Override
    public void onError(String msg) {
        finish();
    }

    @Override
    public void onGetGoodsBySerno(ResponseBean responseBean) {

    }

    @Override
    public void onCheckInventorySuccess(ResponseBean responseBean) {

        SoundPoolUtils.play(context, R.raw.raw_date_complete, 0);
//        goMain();
    }

    /**
     * @author yu
     * @time 2022/1/10  10:33 上午
     * @decs：跳转首页
     */
    private void goMain() {
        Intent intent = new Intent(context, NewMainactivity.class);
        startActivity(intent);
    }

    /**
     * 容错弹框不关闭情况
     */
    private void faultcloseDia() {
        if (timercloseDia != null) {
            timercloseDia.cancel();
        }
        timercloseDia = new CountDownTimer(8000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {
                if (NewEmbActivity.this == null || NewEmbActivity.this.isFinishing() || mCancelled == true)
                    return;
                goMain();
                cancel();

            }
        }.start();

    }

    @Override
    protected void onDestroy() {
        if (timercloseDia != null) timercloseDia.cancel();
        if (timer != null) timer.cancel();
        super.onDestroy();
    }
}